#include<iostream>
#include<jsoncpp/json/json.h>

#define MySelf 1;

const std::string blank_space_sep = " ";
const std::string protocol_sep = "\n";


// "num\n""x + y"
std::string Encode(std::string& content)
{
    std::string package = std::to_string(content.size());
    package += protocol_sep;
    package += content;
    package += protocol_sep;

    return package;
}

// "num\n""x + y\n"
bool Decode(std::string& package,std::string* out)
{
    size_t pos = package.find(protocol_sep);
    if(pos == std::string::npos)    return false;
    std::string len_str = package.substr(0,pos);
    size_t len = std::stoi(len_str);
    size_t total_size = len_str.size() + len + 2;
    if(package.size() < total_size) return false;

    *out = package.substr(pos+1,len);

    package.erase(0,total_size);
    return true;
}


class Request
{
public:
    Request(int data1,int data2,char oper)
        :x(data1)
        ,y(data2)
        ,op(oper)
    {}
    
    Request()
    {}

    ~Request()
    {}

    //"x op y"
    bool Serialize(std::string* out)
    {
#ifdef MySelf
        std::string s = std::to_string(x);
        s += blank_space_sep;
        s += op;
        s += blank_space_sep;
        s += std::to_string(y);  

        *out = s;
        return true;
#else
        Json::Value root;
        root["x"] = x;
        root["y"] = y;
        root["op"] = op;

        Json::FastWriter w;
        *out = w.write(root);
        return true;
#endif
    }

    //"x op y"
    bool DeSerialize(const std::string& in)
    {
#ifdef MySelf
        size_t left = in.find(blank_space_sep);
        if(left == std::string::npos)    return false;
        std::string left_part = in.substr(0,left);

        size_t right = in.rfind(blank_space_sep);
        if(right == std::string::npos)  return false;
        std::string right_part = in.substr(right+1);

        x = std::stoi(left_part);
        y = std::stoi(right_part);
        op = in[left+1];
        return true;
#else
        Json::Value root;
        Json::Reader r;
        r.parse(in,root);

        x = root["x"].asInt();
        y = root["y"].asInt();
        op = root["op"].asInt();

        return true;
#endif
    }

public:
    int x;
    int y;
    char op;
};

class Response
{
public:
    Response(int res,int c)
        :result(res)
        ,code(c)
    {}

    Response()
    {}

    ~Response()
    {}

    //"result code"
    bool Serialize(std::string* out)
    {
#ifdef MySelf
        std::string s = std::to_string(result);
        s += blank_space_sep;
        s += std::to_string(code);  

        *out = s;
        return true;
#else
        Json::Value root;
        root["result"] = result;
        root["code"] = code;

        Json::FastWriter w;
        *out = w.write(root);

        return true;
#endif
    }

    //"result code"
    bool DeSerialize(const std::string& in)
    {
#ifdef MySelf
        size_t pos = in.find(blank_space_sep);
        if(pos == std::string::npos)    return false;
        std::string res = in.substr(0,pos);
        std::string c = in.substr(pos+1);

        result = std::stoi(res);
        code = std::stoi(c);

        return true;
#else
        Json::Value root;
        Json::Reader r;
        r.parse(in,root);

        result = root["result"].asInt();
        code = root["code"].asInt();

        return true;
#endif
    }
public:
    int result;
    int code;
};